package afcp.alumni.action.page;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.security.Secure;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import afcp.alumni.action.base.PageAction;
import afcp.alumni.model.db.Login;
import afcp.alumni.model.db.Member;
import afcp.alumni.service.LoginService;
import afcp.alumni.service.MemberService;
import afcp.alumni.service.SF;

//this roles is associate to the method of PageAction getRoleVisible
@Secure(roles = LoginService.ROLE_ADMIN+","+ LoginService.ROLE_VIP + "," + LoginService.ROLE_MEMBER)
@UrlBinding(value=MemberDetailAction.URL+"/{login}")
public class MemberDetailAction extends PageAction {

	private static final Log logger = LogFactory.getLog(MemberDetailAction.class);
	private static final String JSP = "/WEB-INF/jsp/memberdetail.jsp";
	public static final String URL = "/memberdetail";
	
	private MemberService memberService = SF.member();
	
	private String login;
	private Member member=null;
	
	@Override
	public String getUrl() {
		return URL;
	}

	@Override
	@DefaultHandler
	//this method will be called
	public Resolution view() {
		if ( login == null )
		{
			logger.info("Login parameter not filled in. The user is redirected to the member list page.");
			return new RedirectResolution(MembresAction.URL);
		} else {
			return this.toJsp();
		}
	}

	@Override
	public String getTitle() {
		return msg("memberdetail.title");
	}

	@Override
	protected String defaultJsp() {
		return JSP;
	}

	@Override
	@Before
	protected void setPageVisitedBoolean() {
		setMembres();
	}

//	public Login getLogin() {
//		return id;
//	}

	public void setLogin(String login) {
		this.login = login;
	}

	public Member getMember() {
		if ( member == null )
		{
			Login loginObj = new Login(login);
			member = this.memberService.getMember(loginObj);
			if ( member != null )
			{
				logger.info("member " + member + " found, id is " + member.getId());
			}
			else {
				logger.info("member " + login + " not found !!!!!!");
			}
		}
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}


	
}
